<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>数组扁平化</title>
	</head>
	<body>

	</body>
	<script type="text/javascript">
		let arr = [1, [2, 3, [4, 5, 6],
			[{
				name: '7777'
			}],
			[{
				name: '88888'
			}]
		], 8, 0, [{
			name: '99999'
		}]];
		
		//方法一 flat(需要展开的层级数量)
		console.log(arr.flat(Infinity))

		// 方法二
		let arr2 = JSON.stringify(arr).replace(/\[|\]/g, '')
		console.log(JSON.parse('[' + arr2 + ']'))

		// 方法三（递归）
		const myArr = [];
		const fn = (arr) => {
			for (let i = 0; i < arr.length; i++) {
				if (Array.isArray(arr[i])) {
					fn(arr[i])
				} else {
					myArr.push(arr[i])
				}
			}
		}
		fn(arr)
		console.log(myArr)

		// 方法四（reduce）详见reduce.html
		const newArr = (arr) => {
			return arr.reduce((prev, cur) => {
				return prev.concat(Array.isArray(cur) ? newArr(cur) : cur)
			}, [])
		}
		console.log(newArr(arr))
	</script>
</html>
